package network.server.handler;

import common.util.DBUtil;
import dao.*;
import dao.impl.*;
import module.library.service.impl.*;
import network.server.ClientHandler;
import network.server.RequestDispatcher;
import network.server.handler.BankHandler.BankTransactionHandler;
import network.server.handler.BankHandler.BankAccountHandler;
import network.server.handler.CourseHandler.CourseHandler;
import network.server.handler.LibraryHandler.*;
import network.server.handler.UserHandler.*;
import network.server.handler.StudentHandler.*;
import network.server.handler.StoreHandler.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 初始化所有服务处理器的类，将各模块的请求映射到对应的处理器。
 * <p>
 * 本类负责注册银行、图书馆、用户、学生、课程、商店等模块的所有接口到
 * {@link RequestDispatcher} 中，确保客户端请求可以正确分发到对应处理器。
 * <p>
 * 使用示例：
 * <pre>
 *     initAllHandler handlerInitializer = new initAllHandler();
 *     handlerInitializer.init();
 * </pre>
 */
public class initAllHandler {

    /**
     * 请求分发器实例，用于注册请求和对应的处理器
     */
    RequestDispatcher dispatcher = RequestDispatcher.getInstance();

    /**
     * 初始化所有处理器，将各模块接口与对应处理器进行注册。
     * <p>
     * 包括以下模块：
     * <ul>
     *     <li>银行模块：账户与交易操作</li>
     *     <li>图书馆模块：图书管理、查询、借阅</li>
     *     <li>用户模块：注册、登录、密码管理、邮箱验证</li>
     *     <li>学生模块：学籍信息设置、查询、更新</li>
     *     <li>课程模块：课程管理、选课、排课、查询教师与学生信息</li>
     *     <li>商店模块：购物车、订单、商品、分类操作</li>
     * </ul>
     */
    public void init() {

        // bank Service
        // 账户相关操作
        dispatcher.register("bank.getAccountsByUserId", new BankAccountHandler());
        dispatcher.register("bank.getAccountById", new BankAccountHandler());
        dispatcher.register("bank.recharge", new BankAccountHandler());
        dispatcher.register("bank.withdraw", new BankAccountHandler());
        dispatcher.register("bank.transfer", new BankAccountHandler());
        dispatcher.register("bank.getTransactionHistory", new BankAccountHandler());
        dispatcher.register("bank.createAccount", new BankAccountHandler());
        dispatcher.register("bank.updateAccountName", new BankAccountHandler());
        dispatcher.register("bank.deleteAccount", new BankAccountHandler());

        // 交易相关操作
        dispatcher.register("bank.getTransactions", new BankTransactionHandler());
        dispatcher.register("bank.getAccountTransactions", new BankTransactionHandler());
        dispatcher.register("bank.createTransaction", new BankTransactionHandler());

        // library Service
        dispatcher.register("book.manage", new BookManagementHandler());
        dispatcher.register("book.query", new BookQueryHandler());
        dispatcher.register("book.borrow", new BorrowHandler());

        // login Service
        dispatcher.register("user.Register", new UserServiceHandler());
        dispatcher.register("user.Login", new UserServiceHandler());
        dispatcher.register("user.SendEmail", new UserServiceHandler());
        dispatcher.register("user.FindPassword", new UserServiceHandler());
        dispatcher.register("user.ChangePassword", new UserServiceHandler());
        dispatcher.register("user.CheckPassword", new UserServiceHandler());
        dispatcher.register("student.SetStuStatusA", new StudentServiceHandler());
        dispatcher.register("student.SetStuStatusB", new StudentServiceHandler());
        dispatcher.register("student.SearchStuStatusA", new StudentServiceHandler());
        dispatcher.register("student.SearchStuStatusB", new StudentServiceHandler());
        dispatcher.register("student.UpdateStuStatusA", new StudentServiceHandler());
        dispatcher.register("student.UpdateStuStatusB", new StudentServiceHandler());
        dispatcher.register("student.SearchAllStuStatus", new StudentServiceHandler());

        // course
        dispatcher.register("course.findById", new CourseHandler());
        dispatcher.register("course.findAll", new CourseHandler());
        dispatcher.register("course.findByName", new CourseHandler());
        dispatcher.register("course.findByCourseCode", new CourseHandler());
        dispatcher.register("course.findStatusTrue", new CourseHandler());
        dispatcher.register("course.create", new CourseHandler());
        dispatcher.register("course.update", new CourseHandler());
        dispatcher.register("course.delete", new CourseHandler());
        dispatcher.register("course.enrollStudent", new CourseHandler());
        dispatcher.register("course.dropStudent", new CourseHandler());
        dispatcher.register("course.findStudents", new CourseHandler());
        dispatcher.register("course.apply", new CourseHandler());
        dispatcher.register("course.updateMyCourse", new CourseHandler());
        dispatcher.register("course.findByTeacher", new CourseHandler());
        dispatcher.register("course.schedule", new CourseHandler());
        dispatcher.register("course.findSelectedCourseByStudentId", new CourseHandler());
        dispatcher.register("course.findCourseWithSchedule", new CourseHandler());
        dispatcher.register("course.findTeacherNameByTeacherId", new CourseHandler());

        // course location
        dispatcher.register("location.findLocationById", new CourseHandler());
        dispatcher.register("location.insertLocation", new CourseHandler());
        dispatcher.register("location.findAllLocation", new CourseHandler());
        dispatcher.register("location.updateLocation", new CourseHandler());
        dispatcher.register("location.deleteLocation", new CourseHandler());
        dispatcher.register("location.findLocationByBuilding", new CourseHandler());
        dispatcher.register("location.findLocationByRoomNumber", new CourseHandler());
        dispatcher.register("location.findLocationByBuildingAndRoom", new CourseHandler());
        dispatcher.register("location.getLocationCount", new CourseHandler());

        // Store Service
        /* 购物车主表 */
        dispatcher.register("store.getCartByUserId", new CartHandler());
        dispatcher.register("store.createCart", new CartHandler());
        dispatcher.register("store.clearCart", new CartHandler());
        dispatcher.register("store.getOrCreateCartId", new CartHandler());

        /* 购物车项 */
        dispatcher.register("store.getCartItems", new CartItemHandler());
        dispatcher.register("store.getCartItem", new CartItemHandler());
        dispatcher.register("store.addToCart", new CartItemHandler());
        dispatcher.register("store.updateQuantity", new CartItemHandler());
        dispatcher.register("store.removeFromCart", new CartItemHandler());
        dispatcher.register("store.getItemCount", new CartItemHandler());
        dispatcher.register("store.getTotalAmount", new CartItemHandler());

        /* 分类 */
        dispatcher.register("store.getCategoryById", new CategoryHandler());
        dispatcher.register("store.getCategoriesByName", new CategoryHandler());
        dispatcher.register("store.getAllCategories", new CategoryHandler());
        dispatcher.register("store.getCategoriesByParentId", new CategoryHandler());
        dispatcher.register("store.createCategory", new CategoryHandler());
        dispatcher.register("store.updateCategory", new CategoryHandler());
        dispatcher.register("store.deleteCategory", new CategoryHandler());
        dispatcher.register("store.getCategoryCount", new CategoryHandler());
        dispatcher.register("store.categoryExists", new CategoryHandler());
        dispatcher.register("store.categoryNameExists", new CategoryHandler());
        dispatcher.register("store.hasChildCategories", new CategoryHandler());

        /* 订单主表 */
        dispatcher.register("store.getOrderById", new OrderHandler());
        dispatcher.register("store.getOrderByNumber", new OrderHandler());
        dispatcher.register("store.getOrdersByUser", new OrderHandler());
        dispatcher.register("store.getOrdersByStatus", new OrderHandler());
        dispatcher.register("store.getOrdersByUserAndStatus", new OrderHandler());
        dispatcher.register("store.getAllOrders", new OrderHandler());
        dispatcher.register("store.createOrder", new OrderHandler());
        dispatcher.register("store.updateOrder", new OrderHandler());
        dispatcher.register("store.deleteOrder", new OrderHandler());
        dispatcher.register("store.updateOrderStatus", new OrderHandler());
        dispatcher.register("store.getOrderCount", new OrderHandler());
        dispatcher.register("store.getOrderCountByUser", new OrderHandler());

        /* 订单项 */
        dispatcher.register("store.getOrderItemById", new OrderItemHandler());
        dispatcher.register("store.getOrderItemsByOrderId", new OrderItemHandler());
        dispatcher.register("store.getAllOrderItems", new OrderItemHandler());
        dispatcher.register("store.createOrderItem", new OrderItemHandler());
        dispatcher.register("store.updateOrderItem", new OrderItemHandler());
        dispatcher.register("store.deleteOrderItem", new OrderItemHandler());
        dispatcher.register("store.deleteOrderItemsByOrderId", new OrderItemHandler());
        dispatcher.register("store.getOrderItemCount", new OrderItemHandler());
        dispatcher.register("store.calculateOrderTotal", new OrderItemHandler());
        dispatcher.register("store.createOrderItems", new OrderItemHandler());

        /* 商品 */
        dispatcher.register("store.getProductById", new ProductHandler());
        dispatcher.register("store.getProductByName", new ProductHandler());
        dispatcher.register("store.getAllProducts", new ProductHandler());
        dispatcher.register("store.getProductsByCategoryId", new ProductHandler());
        dispatcher.register("store.getProductsByCategoryName", new ProductHandler());
        dispatcher.register("store.createProduct", new ProductHandler());
        dispatcher.register("store.updateProduct", new ProductHandler());
        dispatcher.register("store.deleteProduct", new ProductHandler());
        dispatcher.register("store.getProductCount", new ProductHandler());
        dispatcher.register("store.productExists", new ProductHandler());
        dispatcher.register("store.productNameExists", new ProductHandler());
        dispatcher.register("store.updateProductStock", new ProductHandler());
        dispatcher.register("store.updateProductStatus", new ProductHandler());
    }
}
